package net.ojbk.study.curriculumDesign.web.handlers.cilentservicer.impl;

import net.ojbk.study.curriculumDesign.beans.cilentservicer.Chat;
import net.ojbk.study.curriculumDesign.beans.cilentservicer.ChatMessage;
import net.ojbk.study.curriculumDesign.beans.usermanage.User;
import net.ojbk.study.curriculumDesign.service.cilentservicer.ICustomerCommService;
import net.ojbk.study.curriculumDesign.service.cilentservicer.ICustomerManaService;
import net.ojbk.study.curriculumDesign.web.handlers.cilentservicer.ICustomerController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: twobox
 * Date: 2019/1/9
 * Time: 22:00
 * Description: No Description
 */
@Controller
@RequestMapping("cilentservicer")
public class CustomerControllerImpl implements ICustomerController {
    @Autowired
    @Qualifier("customerCommService")
    ICustomerCommService customerCommService;

    @Autowired
    @Qualifier("customerManaService")
    ICustomerManaService customerManaService;

    /**
     * 从数据库获取所有的服务记录
     *
     * @param httpSession
     * @return
     */
    @Override
    @RequestMapping("chateds")
    public ResponseEntity<Object> getALlChated(HttpSession httpSession) {
        User user = (User) httpSession.getAttribute("user");
        return new ResponseEntity<Object>(customerManaService.getAllSerRec(user), HttpStatus.OK);
    }

    /**
     * 心跳包，查看是否有新消息
     *
     * @param session
     * @return
     */
    @Override
    @RequestMapping("call")
    public ResponseEntity<Object> isHaveNewMsg(HttpSession session) {
        Chat chat = (Chat) session.getAttribute("cChat");
        try {
            boolean haveNewMsg = customerManaService.isHaveNewMsg(chat);
            return new ResponseEntity<Object>(haveNewMsg, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<Object>("error.", HttpStatus.OK);
        }
    }

    /**
     * 请求一个客服
     *
     * @param httpSession
     * @return
     */
    @Override
    @RequestMapping("reqService")
    public ResponseEntity<Object> requestServicer(HttpSession httpSession) {
        User user = (User) httpSession.getAttribute("user");
        System.out.println("客户请求客服：这个客户：" + user.getUid());
        try {
            // 判断它是否已经有会话
            if (httpSession.getAttribute("cChat") != null)
                throw new Exception("已存在通话。");
            Chat chat = customerCommService.requestService(user);
            httpSession.setAttribute("cChat", chat);
        } catch (Exception e) {
            return new ResponseEntity<Object>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity<Object>(true, HttpStatus.OK);
    }

    /**
     * 获取所有消息的聊天记录
     *
     * @param httpSession
     * @return
     */
    @Override
    @RequestMapping(value = "msgList", method = RequestMethod.GET)
    public ResponseEntity<Object> getAllChatMsg(HttpSession httpSession) {
        Chat chat = (Chat) httpSession.getAttribute("cChat");
        try {
            List<ChatMessage> msgList = customerCommService.getMsgList(chat);
            return new ResponseEntity<Object>(msgList, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<Object>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * 获取一条新消息
     *
     * @param httpSession
     * @return
     */
    @Override
    @RequestMapping(value = "newMsg")
    public ResponseEntity<Object> getNewMsg(HttpSession httpSession) {
        Chat chat = (Chat) httpSession.getAttribute("cChat");
        try {
            ChatMessage newMessage = customerCommService.getNewMessage(chat);
//            "Cache-Control","no-cache"
            HttpHeaders headers = new HttpHeaders();
            headers.set("Cache-Control", "no-cache"); // !! 设置不缓冲
            return new ResponseEntity<Object>(newMessage, headers, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<Object>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * 发送一条消息
     *
     * @param httpSession
     * @param chatMessage json数据
     * @return
     */
    @Override
    @RequestMapping(value = "msg", method = RequestMethod.POST)
    public ResponseEntity<Object> sendMsg(HttpSession httpSession, @RequestBody ChatMessage chatMessage) {
        Chat chat = (Chat) httpSession.getAttribute("cChat");
        try {
            customerCommService.sendMessage(chat, chatMessage);
            return new ResponseEntity<Object>("发送成功", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<Object>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * 断开连接
     *
     * @param httpSession
     * @return
     */
    @Override
    @RequestMapping(value = "chat", method = RequestMethod.PUT)
    public ResponseEntity<Object> catChat(HttpSession httpSession) {
        Chat chat = (Chat) httpSession.getAttribute("cChat");
        try {
            customerCommService.catService(chat);
            return new ResponseEntity<Object>("切断成功", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<Object>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * 发送评分，结束会话
     *
     * @param httpSession
     * @param score
     * @return
     */
    @Override
    @RequestMapping(value = "chat", method = RequestMethod.DELETE)
    public ResponseEntity<Object> endChat(HttpSession httpSession, double score) {
        Chat chat = (Chat) httpSession.getAttribute("cChat");
        try {
            customerCommService.endChat(chat, score);
            httpSession.setAttribute("cChat", null);
            return new ResponseEntity<Object>("评分成功，结束会话。", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<Object>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }

    }
}
